
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO
Imports _2bMatesSite.Common


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for upload_song.
	''' </summary>
	Public Partial Class upload_group_song
		Inherits BaseWebPage
		Protected PicturesTable As System.Web.UI.WebControls.Table
		Protected lblForbiddenUpload As System.Web.UI.WebControls.Label

		Protected Const IMAGES_ON_ROW As Integer = 4
		Protected Const MAX_FILE_SIZE As Integer = 5242880
		' in bytes
		Protected Const MAX_TOTAL_FILE_SIZE As Integer = 7340032
		' in bytes
		Protected heightString As String = ""
		Protected grpID As Integer = -1
		Protected usr As User = Nothing
		Protected grp As Group = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			' An example displaying the first picture
			' this.pageTemplate.PageTitle = PageTitles.UPLOAD_GROUP_SONG_PAGE;
			usr = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.UPLOAD_GROUP_SONG, "", uploadImageForm, Nothing, htPageConstants)
			Me.initAdvertisements(uploadImageForm, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Try
				grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			grp = Groups.GetGroupByID(grpID)

			If usr.ID <> grp.Uid Then
				Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MODERATOR_ACCESS)
			End If

			litSpacer.Text = "<IMG height='8' alt='' src='images/spacer.gif' width=1 border=0 >"

			hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
			hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)

			If Not IsPostBack Then
				BindList()
			End If
		End Sub

		Public Function CreateDataSource() As ICollection
			Dim songsCount As Integer = 0

			If grp.SongURL1 <> Nothing AndAlso grp.SongURL1 <> "" Then
				songsCount += 1
			End If
			If grp.SongURL2 <> Nothing AndAlso grp.SongURL2 <> "" Then
				songsCount += 1
			End If
			If grp.SongURL3 <> Nothing AndAlso grp.SongURL3 <> "" Then
				songsCount += 1
			End If
			If grp.ClipURL <> Nothing AndAlso grp.ClipURL <> "" Then
				songsCount += 1
			End If

			If songsCount = 0 Then
				lblNoImage.Visible = True
			Else
				lblNoImage.Visible = False
			End If

			If songsCount = Constants.MAX_GROUP_MEDIA_UPLOADS Then
				lblName.Visible = False
				txtSongName.Visible = False
				fldFileUpload.Visible = False
				btnUpload.Visible = False
				lblMessage.Visible = True
ElseIf songsCount < Constants.MAX_GROUP_MEDIA_UPLOADS Then
				lblName.Visible = True
				txtSongName.Visible = True
				fldFileUpload.Visible = True
				btnUpload.Visible = True
				lblMessage.Visible = False
			End If

			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("SongURL", GetType(String)))
			dt.Columns.Add(New DataColumn("SongName", GetType(String)))
			dt.Columns.Add(New DataColumn("PlayMusicURL", GetType(String)))
			dt.Columns.Add(New DataColumn("SongID", GetType(String)))

			If grp.SongURL1 <> Nothing AndAlso grp.SongURL1 <> "" Then
				dr = dt.NewRow()

				dr(0) = grp.SongURL1
				dr(1) = grp.SongURLName1
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "1"

				dt.Rows.Add(dr)
			End If

			If grp.SongURL2 <> Nothing AndAlso grp.SongURL2 <> "" Then
				dr = dt.NewRow()

				dr(0) = grp.SongURL2
				dr(1) = grp.SongURLName2
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "2"

				dt.Rows.Add(dr)
			End If

			If grp.SongURL3 <> Nothing AndAlso grp.SongURL3 <> "" Then
				dr = dt.NewRow()

				dr(0) = grp.SongURL3
				dr(1) = grp.SongURLName3
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "3"

				dt.Rows.Add(dr)
			End If

			If grp.ClipURL <> Nothing AndAlso grp.ClipURL <> "" Then
				dr = dt.NewRow()

				dr(0) = grp.ClipURL
				dr(1) = grp.ClipURLName
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "4"

				dt.Rows.Add(dr)
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlPictures.ItemCommand, AddressOf Me.dlPictures_ItemCommand

		End Sub
#End Region

		Public Sub BindList()
			dlPictures.DataSource = CreateDataSource()
			dlPictures.DataBind()
		End Sub

		Public Sub dlPictures_ItemCommand(ByVal Sender As Object, ByVal e As DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim cmd As String = ""

			Dim type As String = src.[GetType]().ToString()
			Select Case type
				Case "System.Web.UI.WebControls.ImageButton"
					cmd = (DirectCast(src, ImageButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.LinkButton"
					cmd = (DirectCast(src, LinkButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.Button"
					cmd = (DirectCast(src, Button)).CommandName
					Exit Select
			End Select
			Select Case cmd
				Case "Remove"
					grp.DeleteSong(e.CommandArgument.ToString(), Page.Server.MapPath(".") + "\uploads\")
					Exit Select
			End Select
			BindList()
		End Sub

		Protected Sub validateFile(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			If (fldFileUpload.PostedFile <> Nothing) AndAlso (fldFileUpload.PostedFile.ContentLength > 0) Then

				Dim fileName As String = args.Value.Trim()
				Dim fileNameInLowerCase As String = fileName.ToLower()
				Dim fileSize As Integer = fldFileUpload.PostedFile.ContentLength

				If fileNameInLowerCase.EndsWith(".wav") OrElse fileNameInLowerCase.EndsWith(".wma") OrElse fileNameInLowerCase.EndsWith(".mp3") OrElse fileNameInLowerCase.EndsWith(".avi") OrElse fileNameInLowerCase.EndsWith(".mpg") OrElse fileNameInLowerCase.EndsWith(".mpeg") Then
					If fileSize > MAX_FILE_SIZE Then
						valFileFormat.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_MsgErrMaxFileSize_TEXT")
						args.IsValid = False
					End If
				Else
					args.IsValid = False
				End If
			Else
				args.IsValid = False
			End If
		End Sub

		Protected Sub UploadImage(ByVal file As HttpPostedFile, ByVal grp As Group)
			Dim splitFileName As String() = file.FileName.Split(New Char() {"/"C, "\"C})
			Dim sFileName As String = System.IO.Path.GetFileName(file.FileName)
			Dim sFileExtension As String = sFileName.Substring(sFileName.LastIndexOf("."))
			Dim bMachFound As Boolean = False

			If bMachFound = False AndAlso (grp.SongURL1 = Nothing OrElse grp.SongURL1 = "") Then
				sFileName = grp.ID.ToString() + "_grp_song1" + sFileExtension
				grp.SongURL1 = sFileName
				grp.SongURLName1 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = False AndAlso (grp.SongURL2 = Nothing OrElse grp.SongURL2 = "") Then
				sFileName = grp.ID.ToString() + "_grp_song2" + sFileExtension
				grp.SongURL2 = sFileName
				grp.SongURLName2 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = False AndAlso (grp.SongURL3 = Nothing OrElse grp.SongURL3 = "") Then
				sFileName = grp.ID.ToString() + "_grp_song3" + sFileExtension
				grp.SongURL3 = sFileName
				grp.SongURLName3 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = False AndAlso (grp.ClipURL = Nothing OrElse grp.ClipURL = "") Then
				sFileName = grp.ID.ToString() + "_grp_clip" + sFileExtension
				grp.ClipURL = sFileName
				grp.ClipURLName = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = True Then
				Dim sSaveLocation As String = Page.Server.MapPath(".") + "\uploads\" + sFileName

				file.SaveAs(sSaveLocation)

				grp.Save()
			End If
		End Sub

		Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				UploadImage(fldFileUpload.PostedFile, grp)

				tblPhotos.Visible = True
				Response.Redirect(Constants.UPLOAD_GROUP_SONG + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			End If
		End Sub

		Private Sub valFileFormat_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable uploadImageForm.
		====================
		Public ReadOnly Property uploadImageForm() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_uploadImageForm
			End Get
		End Property
		'set { Migrated_uploadImageForm = value; }
	End Class
End Namespace






